// ChunkDoc.cpp : implementation file
//

#include "stdafx.h"
#include "U7FileTool.h"
#include "ChunkDoc.h"

#include "U7FileDefs.h"

// CChunkDoc

IMPLEMENT_DYNCREATE(CChunkDoc, CDocument)

CChunkDoc::CChunkDoc()
{
	m_pChunkFile = NULL;
}

BOOL CChunkDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
	return TRUE;
}

CChunkDoc::~CChunkDoc()
{
	if ( m_pChunkFile )
		delete [] m_pChunkFile;
}


BEGIN_MESSAGE_MAP(CChunkDoc, CDocument)
END_MESSAGE_MAP()


// CChunkDoc diagnostics

#ifdef _DEBUG
void CChunkDoc::AssertValid() const
{
	CDocument::AssertValid();
}

#ifndef _WIN32_WCE
void CChunkDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif
#endif //_DEBUG

#ifndef _WIN32_WCE
// CChunkDoc serialization

void CChunkDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
		CFile* pFile = ar.GetFile();
		m_lChunkFileLength = (LONG)pFile->GetLength();

		if ( m_lChunkFileLength % sizeof(CHUNK) != 0 )
			AfxThrowFileException(CFileException::genericException,100);

		m_nChunkCount = m_lChunkFileLength / sizeof(CHUNK);

		m_pChunkFile = new BYTE[m_lChunkFileLength];
		if ( !m_pChunkFile )
			AfxThrowMemoryException();

		pFile->Read( m_pChunkFile, m_lChunkFileLength );
	}
}
#endif


// CChunkDoc commands


void CChunkDoc::DeleteContents()
{
	// TODO: Add your specialized code here and/or call the base class
	if ( m_pChunkFile )
	{
		delete [] m_pChunkFile;
		m_pChunkFile = NULL;
	}

	CDocument::DeleteContents();
}
